package daoImp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.opensymphony.xwork2.Result;
import com.sun.crypto.provider.RSACipher;

import DBUtil.DBConnection;
import bean.Vote;
import dao.VoteDao;

public class VoteDaoImp implements VoteDao {

	@Override
	public void addVote(Vote vote) {
		Connection conn = DBConnection.getConnection();
		String addSql = "insert into tb_vote(voteName,channelID) values(?,?)";
		PreparedStatement pStatement = null;

		try {
			pStatement = conn.prepareStatement(addSql);
			pStatement.setString(1, vote.getVoitName());
			pStatement.setInt(2, vote.getChannelID());
			pStatement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				DBConnection.closeStatement(pStatement);
				DBConnection.closeConnection(conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public void delVote(int voteId) {

	}

	@Override
	public Vote findVoteByName(String name) {
		Connection conn = DBConnection.getConnection();
		String querySql = "select  * from tb_vote where voteName=?";
		PreparedStatement pStatement = null;
		ResultSet resultSet=null;
		Vote vote=null;
		try {
			pStatement = conn.prepareStatement(querySql);
			pStatement.setString(1, name);
			resultSet =pStatement.executeQuery();
			if(resultSet.next()) {
				vote=new Vote();
				vote.setChannelID(resultSet.getInt(1));
				vote.setVoitName(resultSet.getString(2));
				vote.setVoteID(resultSet.getInt(3));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				DBConnection.closeResultSet(resultSet);
				DBConnection.closeStatement(pStatement);
				DBConnection.closeConnection(conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}

		return null;
	}

}
